package com.infore.siteManagement.repository;

import java.util.List;

import org.springframework.data.jpa.repository.EntityGraph;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.infore.siteManagement.entity.Area;

import feign.Param;
/**
 * 区域仓库
 * @author 刘乐法
 *
 *
 */
@Repository
public interface IAreaRespository extends JpaRepository<Area, String>,JpaSpecificationExecutor<Area>{

	@EntityGraph(value = "area_view")
	List<Area> findByAreaLevelAreaLevelCode(String code);
	
	@Modifying
	@Transactional
	int deleteByAreaIdIn(List<String> ids);
	
	@EntityGraph(value = "area_view")
	List<Area> findAllByAreaCodeIn(List<String> areaCode);
	
	@EntityGraph(value = "area_view")
	List<Area> findAllByAreaCode(String areaCode);
	
	/**递归查询父区域**/
	//@Query(value = "select distinct * from AREA t start with t.area_code in(:areaCode) connect by prior t.parent_id =  t.area_code",nativeQuery=true)
	
	@EntityGraph(value = "area_view")
    List<Area> findAll();
	
	@EntityGraph(value = "area_view")
	List<Area> findByAreaCodeIn(@Param("areaCode")List<String> areaCode);
	
	@EntityGraph(value = "area_view")
	List<Area> findByAreaLevelAreaLevelId(String is);
	
	@EntityGraph(value = "area_view")
	List<Area> findByParentId(String name);
	
	Area findByAreaId(String areaId);
	
	Area findOneByAreaCode(String areacode);
	
	/**查出与关键词相关的区域**/
	@Query(value = "select distinct * from AREA t start with t.area_code in(select m.area_code from AREA m where m.area_name like :areaName) connect by prior t.area_code =  t.parent_id " + 
			"union " + 
			"select distinct * from AREA t start with t.area_code in(select m.area_code from AREA m where m.area_name like :areaName) connect by prior t.parent_id = t.area_code",nativeQuery = true)
	List<Area> queryAllByKeyName(@Param("areaName")String areaName);
	
	
	@Query(value = "select distinct * from AREA t start with t.area_code in(:areaCode) connect by prior t.parent_id =  t.area_code",nativeQuery=true)
	List<Area> queryAreaParent(@Param("areaCode")List<String> areaCode);
}
